﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Mechanical;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.LIB;

namespace YArchitech.HVAC
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class CreatDuctInsulation : IExternalCommand
	{
		[DllImport("user32.dll", EntryPoint = "SendMessageA")]
		public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);

		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			if (commandData == null)
			{
				throw new System.ArgumentNullException("commandData", "is null");
			}
			Transaction transaction = null;
			FormCreatDuctInsulation formCreatDuctInsulation = new FormCreatDuctInsulation();
			Result result;
			try
			{
				Document document = commandData.Application.ActiveUIDocument.Document;
				UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
				Selection selection = activeUIDocument.Selection;
				if (CreatDuctInsulation.hWndRevit == null)
				{
					CreatDuctInsulation.hWndRevit = new YJKWindowHandle(Process.GetCurrentProcess().MainWindowHandle);
				}
				formCreatDuctInsulation.Show(CreatDuctInsulation.hWndRevit);
				int wMsg = 7;
				CreatDuctInsulation.SendMessage(CreatDuctInsulation.hWndRevit.Handle, wMsg, IntPtr.Zero, IntPtr.Zero);
				List<Duct> list = new List<Duct>();
				List<FamilyInstance> list2 = new List<FamilyInstance>();
				DuctInsulationManage ductInsulationManage = new DuctInsulationManage(activeUIDocument);
				transaction = new Transaction(document, "创建保温层类型");
				transaction.Start();
				ductInsulationManage.CreatInsulationType();
				transaction.Commit();
				for (;;)
				{
					string ductSystemName = ductInsulationManage.GetDuctSystemName();
					if (string.IsNullOrEmpty(ductSystemName))
					{
						break;
					}
					for (;;)
					{
						try
						{
							list = ductInsulationManage.GetCalcInsulationDucts(ductSystemName, ref list2);
							if ((list == null || list.Count < 1) && (list2 == null || list2.Count < 1))
							{
								continue;
							}
							double insulationThickness = formCreatDuctInsulation.GetInsulationThickness();
							InsulationLayerParam settingMaterial = formCreatDuctInsulation.GetSettingMaterial();
							transaction = new Transaction(document, "添加管道保温层");
							transaction.Start();
							DuctInsulationType insulationTypeByName = ductInsulationManage.GetInsulationTypeByName(settingMaterial.Name);
							ductInsulationManage.AddDuctsInsulationLayer(insulationTypeByName, list, insulationThickness);
							ductInsulationManage.AddDuctsInsulationLayer(insulationTypeByName, list2, insulationThickness);
							transaction.Commit();
							continue;
						}
						catch (Exception ex)
						{
							string message2 = ex.Message;
							break;
						}
						goto IL_16C;
					}
				}
				IL_16C:
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex2)
			{
				MessageBox.Show(ex2.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				formCreatDuctInsulation.Close();
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}

		public static YJKWindowHandle hWndRevit;
	}
}
